﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.JuicyLib.MarketData.Structure;

namespace Algobox.JuicyLib.Apps.EODData.Files
{
    public class EOD : Dictionary<string, OHLC>
    {
        public EOD(ref string file)
        {
            LoadFile(ref file);
        }

        public const string FILENAME_LSE_EOD = "ftp://ftp.eoddata.com/LSE_{0:yyyyMMdd}.txt";
        public const string FILENAME_MLSE_EOD = "ftp://ftp.eoddata.com/MLSE_{0:yyyyMMdd}.txt";
        public const string FILENAME_FOREX_EOD = "ftp://ftp.eoddata.com/FOREX_{0:yyyyMMdd}.txt";
        public const string FILENAME_NYSE_EOD = "ftp://ftp.eoddata.com/NYSE_{0:yyyyMMdd}.txt";
        public const string FILENAME_NASDAQ_EOD = "ftp://ftp.eoddata.com/NASDAQ_{0:yyyyMMdd}.txt";
        public const string FILENAME_AMEX_EOD = "ftp://ftp.eoddata.com/AMEX_{0:yyyyMMdd}.txt";
        public const string FILENAME_OPRA_EOD = "ftp://ftp.eoddata.com/OPRA_{0:yyyyMMdd}.txt";
        public const string FILENAME_TSX_EOD = "ftp://ftp.eoddata.com/TSX_{0:yyyyMMdd}.txt";
        public const string FILENAME_TSXV_EOD = "ftp://ftp.eoddata.com/TSXV_{0:yyyyMMdd}.txt";

        const char EOD_SEPERATOR = ',';
        const int EOD_COLUMNS = 7;

        const int EOD_SYMBOL = 0;
        const int EOD_DATE = 1;
        const int EOD_OPEN = 2;
        const int EOD_HIGH = 3;
        const int EOD_LOW = 4;
        const int EOD_CLOSE = 5;
        const int EOD_VOLUME = 6;

        void LoadFile(ref string file)
        {
            try
            {
                OHLC ohlc;
                string[] elements; 
                decimal price;
                long volume;
                string[] lines = file.Split('\n');
                string symbol;
                foreach (string line in lines)
                {
                    elements = line.Split(EOD_SEPERATOR);

                    symbol = elements[EOD_SYMBOL];
                    
                    if (elements.Length < EOD_COLUMNS)
                        continue;

                    ohlc = new OHLC();

                    if (!Decimal.TryParse(elements[EOD_OPEN], out price))
                        continue;
                    ohlc.Open = price;

                    if (!Decimal.TryParse(elements[EOD_HIGH], out price))
                        continue;
                    ohlc.High = price;

                    if (!Decimal.TryParse(elements[EOD_LOW], out price))
                        continue;
                    ohlc.Low = price;

                    if (!Decimal.TryParse(elements[EOD_CLOSE], out price))
                        continue;
                    ohlc.Close = price;

                    if (!Int64.TryParse(elements[EOD_VOLUME], out volume))
                        continue;
                    ohlc.Volume = volume;

                    // save this ohlc element
                    if (!this.ContainsKey(symbol))
                        this.Add(symbol, ohlc);
                    else
                        Console.WriteLine("{0} exists OLD({1}) NEW({2})", symbol, this[symbol], ohlc);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }


    }
}
